#! python3
# 用 BeautifulSoup 模块解析 HTML

import bs4
import requests
import json

try:

    # exampleFile = open('example.html')
    # exampleSoup = bs4.BeautifulSoup(exampleFile, features='html.parser')
    # print(exampleSoup)
    # exampleFile.close()

    response = requests.get('http://www.baidu.com/')
    response.raise_for_status()

    response.encoding = 'utf-8'

    soup = bs4.BeautifulSoup(response.text, features='html.parser')
    print(soup)

    print('\n=====================用 select()方法寻找元素==================')
    # elements = soup.select('div')  # 名为<div>的元素
    # elements = soup.select('#author')  # 带有id属性为author的元素
    # elements = soup.select('.notice')  # class属性名为notice的元素
    # elements = soup.select('div span')  # 在<div>元素之内的<span>元素
    # elements = soup.select('div > span')  # 直接在<div>元素之内的<span>元素，中间没有其他元素
    # elements = soup.select('input[name]')  # 存在name属性的<input>元素
    # elements = soup.select('input[type="button"]')  # 存在type=button的<input>元素

    # elements = soup.select('.mnav') # class属性名为mnav的元素
    elements = soup.select('a[name="tj_trhao123"]')  # 存在name属性的<a>元素
    print(elements)  # 符合条件的结果列表
    element = elements[0]
    print(element.getText())  # 元素的文本或内部的HTML
    print(str(element))  # 元素的完整内容
    print(element.attrs)  # 元素属性和属性值的字典

    print('\n=====================通过元素的属性获取数据==================')
    print(element.get('name'))
    print(element.get('href'))
    print(element.get('class'))
    print(element.get('daiquan') is None)

except Exception as exc:
    print('There was a problem: %s' % exc)
